<?php
class Debug
{
	static function dbg_style()
	{
		static $style;
		if(!$style)
		{
			$style	=	1;
			echo '<style>.dbg{text-align:left;font-size:13px;background-color:#fff;color:green; word-wrap:break-word;white-space:pre-wrap;}</style>';
		}
	}
	static function pre($o, $e=0, $t=1)
	{
		self::dbg_style();
		echo '<pre class="dbg">';
		if($t){
			echo htmlspecialchars(print_r($o, 1));
		}
		else{
			print_r($o);
		}
		echo '</pre>';
		if($e) die;
	}
	static function dump($o, $e=0)
	{
		self::dbg_style();
		echo '<pre class="dbg">';
		var_dump($o);
		echo '</pre>';
		if($e) die;
	}
	
	static function file($o, $tp=0, $name='dbg_file.log')
	{
		file_put_contents(DATA_PATH.'/'.$name, ($tp?self::str($o):print_r($o, true))."\n", FILE_APPEND);
	}
	
	/**
	 * 替代 print_r(xx,true)的用法。
	 * 因为print_r取返回值的方法是用到ob_buffer，在自定义的ob_handler里会出错
	 */
	static function str($o, $lv=0)
	{
		if(is_object($o))
			$o	=	get_object_vars($o);
		if(is_array($o))
		{
			$head	=	str_repeat("\t", $lv);
			$tmp	=	"array (\n";
			foreach($o as $k=>$v)
			{
				$tmp	.=	("{$head}\t[$k]\t=>  ".self::str($v, $lv+1)."\n");
			}
			return $tmp."$head)";
		}
		else
		{
			return $o;
		}
	}
	
}
?>